//
//  BaseVC.m
//  OC_Poj_demo
//
//  Created by weixin on 2018/7/17.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "BaseVC.h"


@interface BaseVC ()

@property (nonatomic, assign) UIStatusBarStyle statusBarStyle;

@end

@implementation BaseVC

- (void)dealloc {
    NSLog(@"控制器释放 ----- %@",self);
   
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.baseNavigationView];
    [self.view bringSubviewToFront:self.baseNavigationView];
    
}


#pragma mark - 导航栏方法
- (void)setNavigationBarHide {
    [self.baseNavigationView setHidden:YES];
}
- (void)setNavigationTitle:(NSString *)title {
    self.baseNavigationView.titleLabel.text = title;
}
- (void)setScrollViewInsets:(UIScrollView *)scrollView {
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        // Fallback on earlier versions
        self.automaticallyAdjustsScrollViewInsets = NO;
        
    }
}
- (void)setStatusBar:(UIStatusBarStyle)style {
    self.statusBarStyle = style;
    [self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return self.statusBarStyle;
}



#pragma mark - Setter
- (void)setNavigationViewIsShowShadow:(BOOL)navigationViewIsShowShadow {
    _navigationViewIsShowShadow = navigationViewIsShowShadow;
    UIColor *color = [UIColor clearColor];
    if (_navigationViewIsShowShadow) {
        color = [UIColor defaultShadowColor];
    }
    [self.baseNavigationView setShadowType1WithColor:color offset:CGSizeMake(0, 5) opacity:1 radius:3];
}


- (BaseNavigationView *)baseNavigationView {
    if (!_baseNavigationView) {
        _baseNavigationView = [BaseNavigationView instanceWithFrame:CGRectMake(0, 0, kScreenW, NaviHeight)];
        // 根控制器Pop按钮
        NSArray<NSString *> *rootClassName = @[@"MainVC"];
        if ([rootClassName containsObject:NSStringFromClass([self class])]) {
            [_baseNavigationView.leftButton1 setHidden:YES];
        }
        
        __weak __typeof(self)weakSelf = self;
        // 按钮点击
        _baseNavigationView.navigationViewClickBlock = ^(NavigationViewClickType clickType, UIButton *button) {
            switch (clickType) {
                case NavigationViewClickTypeLeft1: {
                    [weakSelf leftButton1ClickWithButton:button];
                } break;
                case NavigationViewClickTypeRight1: {
                    [weakSelf rightButton1ClickWithButton:button];
                } break;
                case NavigationViewClickTypeRight2: {
                    [weakSelf rightButton2ClickWithButton:button];
                } break;
                case NavigationViewClickTypeNone: {
                    
                } break;
            }
        };
    }
    return _baseNavigationView;
}







#pragma mark - 导航栏按钮Click
- (void)leftButton1ClickWithButton:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightButton1ClickWithButton:(UIButton *)button {
    
}
- (void)rightButton2ClickWithButton:(UIButton *)button {
    
}






#pragma mark - 根据内容数量是否显示无数据背景图
- (void)showNoneDataViewWithFrame:(CGRect)frame title:(NSString *)title modelCount:(NSInteger)count {
    // 数量大于0 则不显示
    if (count > 0) {
        [self.noneDataView setHidden:YES];
    } else {
        
        /* TODO: ================= 判断是否显示背景图片 =================*/

        if (0) {
            if (CGRectIsEmpty(frame)) {
                frame = CGRectMake(0, NaviHeight, kScreenW, kScreenH - NaviHeight);
            }
            if (!title) {
                title = @"暂无数据";
            }
            self.noneDataView.frame = frame;
            self.noneDataView.titleLabel.text = title;
            [self.noneDataView setHidden:NO];
        }
    }
}
- (BaseVCNoneDataView *)noneDataView {
    if (!_noneDataView) {
        _noneDataView = [[BaseVCNoneDataView alloc] init];
        //        _noneDataView.titleLabel.text = title;
        [self.view addSubview:_noneDataView];
    }
    return _noneDataView;
}
- (BaseContrastIconView *)contrastIconView {
    if (!_contrastIconView) {
        _contrastIconView = [BaseContrastIconView instance];
        [self.view addSubview:_contrastIconView];
        [self.view bringSubviewToFront:_contrastIconView];
    }
    return _contrastIconView;
}







#pragma mark - 导航控制器释放 在viewDidAppear使用
- (void)releaseNavigationControllerVCArray:(NSArray<Class> *)toBeReleaseVCArray {
    if (!toBeReleaseVCArray || toBeReleaseVCArray.count == 0) {
        return;
    }
    if (self.navigationController) {
        NSMutableArray<UIViewController *> *vcArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
        NSMutableIndexSet *mSet = [[NSMutableIndexSet alloc] init];
        for (int i=0; i<vcArray.count - 1; i++) {
            UIViewController *subVC = vcArray[i];
            for (int j=0; j<toBeReleaseVCArray.count; j++) {
                //移除控制器
                if ([subVC isKindOfClass:toBeReleaseVCArray[j]]) {
                    [mSet addIndex:i];
                    break;
                }
            }
        }
        if (mSet.count > 0) {
            [vcArray removeObjectsAtIndexes:mSet];
        }
        self.navigationController.viewControllers = vcArray;
    }
}








#pragma mark - 如果token 不一致删除数据
- (void)errorTokenHandleWithError:(NSError *)error vc:(BaseVC *)vc completion:(EmptyCallback)completion {
    
    if (error && error.code == 10) {
        kUSER_INFO_MANAGER.refresh = YES;
        [kUSER_INFO_MANAGER removeUserInfo];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (completion) {
                completion();
                return ;
            }
            [vc.navigationController popViewControllerAnimated:YES];
        });
    } else {
//        [MCTips showErrorInfoWithError:error view:vc.view];
    }
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (self.navigationController.viewControllers.count > 1) {
        self.tabBarController.tabBar.hidden = YES;
    }else{
        self.tabBarController.tabBar.hidden = NO;
    }
    self.navigationController.navigationBarHidden = YES;

}



#pragma mark - 生命周期
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    //自带导航栏
//    self.navigationController.navigationBarHidden = YES;
//    
//    //    [MobClick beginLogPageView:[Tools currentVCNameWithClass:[self class]]];
//}




- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //    [MobClick endLogPageView:[Tools currentVCNameWithClass:[self class]]];
    // 控制器消失时将状态栏设回默认
    [self setStatusBar:(UIStatusBarStyleDefault)];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    
}




// 新增生命周期
- (void)viewSafeAreaInsetsDidChange {
    [super viewSafeAreaInsetsDidChange];
    if (@available (iOS 11.0 , *)) {
        
    }
}

-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self.view bringSubviewToFront:self.baseNavigationView];
    //显示阴影让自定义导航栏始终显示最顶层
    //    [self.view bringSubviewToFront:_view];
}
@end
